package cn.tedu.demo.controller;

import cn.tedu.demo.mapper.AdminMapper;
import cn.tedu.demo.pojo.dto.EmpLoginDTO;
import cn.tedu.demo.pojo.vo.EmpVO;
import cn.tedu.demo.service.impl.AdminServiceImpl;
import cn.tedu.demo.web.JsonResult;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/admins")
@Api(tags = "1.管理员版块")
public class AdminController {
    @Autowired
    private AdminServiceImpl adminService;

    public AdminController(){log.debug("开始加载管理员控制器类");}

    @PostMapping("/login")
    @ApiOperation("1.用户登录")
    @ApiOperationSupport(order = 100)
    public JsonResult<Void> login(@RequestBody EmpLoginDTO empLoginDTO,HttpSession session ){

        log.debug("开始处理【管理员登录】的请求，参数：{}",empLoginDTO);
        adminService.login(empLoginDTO,session);
        return JsonResult.ok();
    }

    @GetMapping("/currentAdmin")
    public EmpVO currentEmp(HttpSession session){
        return  (EmpVO) session.getAttribute("admin");//取出会话对象并返回，Obj类型需要强转
    }

    @GetMapping("/logout")
    public void logout(HttpSession session){
        session.removeAttribute("admin");//登出，移除会话对象
    }

}
